//
//  YZUploadAPI.m
//  VET
//
//  Created by Lam BG on 2018/6/26.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZUploadAPI.h"
#import "YZUser.h"
#import "YZNetworkManager.h"

@implementation YZUploadAPI

+ (RACSignal*)uploadImage:(UIImage*)image imgPath:(NSString*)path apiKey:(NSString*)key {
    
    NSString *uid = [YZUser sharedInstance].uid;
    YZUploadAPI *api = [YZUploadAPI api];
    api.action = [NSString stringWithFormat:@"%@/%@", @"/api/user/headIcon", uid];
    api.showLoadingStatus = YES;
    
    return [api uploadImage:image imgPath:path apiKey:key];
}

- (RACSignal*)uploadImage:(UIImage*)image imgPath:(NSString*)path apiKey:(NSString*)key {
    
    return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        // 是否显示菊花
        [self requestBegin];
        NSString *url = [NSString stringWithFormat:@"%@%@", self.urlDomain, self.action];
        [[[YZNetworkManager sharedInstance] uploadImgWithUrl:url params:self.params withHeaders:self.headerParams img:image imgPath:path apiKey:key] subscribeNext:^(id  _Nullable data) {
            
            [subscriber sendNext:data];
            
        } error:^(NSError * _Nullable error) {
            
#ifdef DEBUG
            // debug 时 显示错误信息
            [self showErrorInfo:error];
#endif
            [self requestEnd];
            [subscriber sendError:error];
            
        } completed:^{
            
            [self requestEnd];
            [subscriber sendCompleted];
        }];
        
        return [RACDisposable disposableWithBlock:^(){}];
    }];
}

@end
